//
//  HomeTableViewController.swift
//  Weibo
//
//  Created by 。。 on 16/4/11.
//  Copyright © 2016年 Lizhuo. All rights reserved.
//

import UIKit

let LZReuseHomeTableView = "LZReuseHomeTableView"

class HomeTableViewController: BaseTableViewController {

    var statues = [Status]() {
        didSet {
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.如果没有登录，就设置未登录界面的信息
        if !userLogin {
            visitorView?.setupVistorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //2.初始化导航条
        setupNav()
        
        //3.注册通知，监听菜单
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillDismiss, object: nil)
        
        //4.加载微博数据
        loadData()
        
        //注册Cell
        tableView.registerClass(StatusTableViewCell.self, forCellReuseIdentifier: LZReuseHomeTableView)
    }
    
    /**
     获取微博数据
     */
    func loadData() {
        Status.loadStatuses { (models, error) in
            if error != nil {
                return
            }
            self.statues = models!
        }
    }
    
    //MARK: - 监听的通知
    /**
     修改标题按钮的状态
     */
    func change() {
        let titleBtn = navigationItem.titleView as! TitleButton
        titleBtn.selected = !titleBtn.selected
    }
    
    deinit {
        //移除通知
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //MARK: - 初始化导航条
    private func setupNav() {
        //1. 左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftItemClick))
        //2. 右边按钮
        navigationItem.rightBarButtonItem =
            UIBarButtonItem.creatBarButtonItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightItemClick))
        
        //3.初始化标题按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("李卓小怪兽 ", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: #selector(HomeTableViewController.titleBtnClick(_:)), forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    func titleBtnClick(btn: TitleButton) {
        //1.修改箭头方向
//        btn.selected = !btn.selected
        //2.弹出菜单
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //2.1设置转场代理
        vc?.transitioningDelegate = popoverAnimator
        //2.2设置转场的样式
        vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
        presentViewController(vc!, animated: true, completion: nil)
    }
    
    func leftItemClick() {
        print(#function)
    }
    
    func rightItemClick() {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        presentViewController(vc!, animated: true, completion: nil)
    }

    //MARK: - 懒加载
    //一定要定义一个属性来保存自定义转场动画，不然会出错
    private lazy var popoverAnimator: PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.presentFrame = CGRect(x: 100, y: 56, width: 200, height: 350)
        return pa
    }()
}

extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statues.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //1.获取cell
        let cell = tableView.dequeueReusableCellWithIdentifier(LZReuseHomeTableView, forIndexPath: indexPath) as! StatusTableViewCell
        //2.设置数据
        cell.status = statues[indexPath.row]
        //3.返回cell
        return cell
    }
}
